package com.yxhl.zoume.core.user.presenter.passenger;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.user.view.passenger.UpdatePassengerView;
import com.yxhl.zoume.data.http.model.passenger.ZMPassenger;
import com.yxhl.zoume.data.http.rest.param.passenger.PassengerParam;
import com.yxhl.zoume.data.http.rest.param.passenger.UpdatePassengerParam;
import com.yxhl.zoume.data.http.rest.response.passenger.UpdatePassengerResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.passenger.UpdatePassengerUseCase;

import javax.inject.Inject;

/**
 * Created by sugarya on 16/3/26.
 */
public class UpdatePassengerPresenter extends BasePresenter implements IPresenter {

    private UpdatePassengerUseCase updatePassengerUseCase;
    private UpdatePassengerView mUpdatePassengerView;

    @Inject
    public UpdatePassengerPresenter(UpdatePassengerUseCase updatePassengerUseCase, Context context) {
        super(context);
        this.updatePassengerUseCase = updatePassengerUseCase;
    }

    /**
     * 确定修改乘客人信息 网络请求回调
     *
     * @param passengerParam
     */
    public void confirmUpdatePassenger(PassengerParam passengerParam) {
        if (passengerParam != null) {
            final UpdatePassengerParam updatePassengerParam = new UpdatePassengerParam(
                    getMobile(),
                    getToken(),
                    passengerParam
            );
            updatePassengerUseCase.execute(updatePassengerParam,new ZMSubscriber<UpdatePassengerResponse>() {
                @Override
                public void onCompleted() {
                    super.onCompleted();
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    mUpdatePassengerView.renderDataOnError();
                }

                @Override
                public void onNext(UpdatePassengerResponse updatePassengerResponse) {
                    super.onNext(updatePassengerResponse);
                    if (updatePassengerResponse != null) {
                        boolean success = updatePassengerResponse.isSucc();
                        if (success) {
                            ZMPassenger zmPassenger = updatePassengerResponse.getPassenger();
                            mUpdatePassengerView.renderDataOnSuccess(zmPassenger);
                        } else {
                            String resultMessage = updatePassengerResponse.getResultMessage();
                            mUpdatePassengerView.renderDataOnFailure(resultMessage);
                        }
                    }
                }
            });
        }
    }

    @Override
    public void onResume() {
    }

    @Override
    public void onPause() {
    }

    @Override
    public void onDestroy() {
        updatePassengerUseCase.unsubscribe();
    }

    @Override
    public void attachView(BaseView view) {
        if (view != null) {
            if (view instanceof UpdatePassengerView) {
                mUpdatePassengerView = (UpdatePassengerView) view;
            }
        }
    }
}
